/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.KelasDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.dao.impl.KelasDAOImpl;
import com.ivanbiz.model.Kelas;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class DaftarKelasLaporanService extends SelectorComposer<Component> {

    @Wire
    Window winDaftarKelasLaporan;
    KelasDAO kelasDAO;
    SimpleDateFormat sdf;
    SimpleDateFormat simpleDateFormat;
    SimpleDateFormat simpleDateFormatFrom;
    @Wire
    Listbox listKelas;
    DaftarKelasDAO daftarKelasDAO;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        kelasDAO = new KelasDAOImpl();
        daftarKelasDAO = new DaftarKelasDAOImpl();
        sdf = new SimpleDateFormat("dd MMM yyyy");
        loadKelas();
    }

    private void loadKelas() {
        try {
            listKelas.getItems().clear();
            int no = 0;
            int x = 0;
            List<Kelas> loadKelas = kelasDAO.getAll(Kelas.class);
            for (Kelas kelas : loadKelas) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(kelas);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(kelas.getTransactionReference()));
                item.appendChild(new Listcell(kelas.getDeskripsi()));
                item.appendChild(new Listcell(kelas.getPengajar().getNama()));
                item.appendChild(new Listcell(sdf.format(kelas.getTanggalKelas())));
                item.appendChild(new Listcell(kelas.getTanggalKelas2() == null ? "" : sdf.format(kelas.getTanggalKelas2())));
                item.appendChild(new Listcell(kelas.getTempatKelas()));
                item.appendChild(new Listcell(kelas.getAlamatKelas()));
                listKelas.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(KelasService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#lihat;")
    public void lihat() {
        Kelas kelas = listKelas.getSelectedItem().getValue();
        Map map = new HashMap();
        map.put("kelas", kelas);
        winDaftarKelasLaporan.detach();
        Window win = (Window) Executions.createComponents("/daftarKelasLaporanView.zul", Path.getComponent("/menu/contentDiv"), map);
    }

    @Listen("onClick=#print;")
    public void print() {
        Kelas kelas = listKelas.getSelectedItem().getValue();
        Map map = new HashMap();
        map.put("kelas", kelas);
        Window win = (Window) Executions.createComponents("/daftarKelasLaporanPrint.zul", null, map);
        win.doModal();
    }
}
